package domain;

import dao.FileDAO;
import gui.GUIUtils;
import java.util.HashSet;
import java.util.Set;

public class File {
    private Integer id_file;
    private Long ultima_modifica;
    private String path_file;
    private Boolean incluso;
    private TipoLinkIn linkIn = null;
    /**
     * @return the id_file
     */
    public Integer getId_file() {
        return id_file;
    }
    /**
     * @param id_file the id_file to set
     */
    public void setId_file(Integer id_file) {
        this.id_file = id_file;
    }
    /**
     * @return the ultima_modifica
     */
    public Long getUltima_modifica() {
        return ultima_modifica;
    }

    /**
     * @param ultima_modifica the ultima_modifica to set
     */
    public File setUltima_modifica(Long ultima_modifica) {
        this.ultima_modifica = ultima_modifica;
        return this;
    }

    /**
     * @return the path_file
     */
    public String getPath_file() {
        return path_file;
    }

    /**
     * @param path_file the path_file to set
     */
    public File setPath_file(String path_file) {
        this.path_file = path_file;
        return this;
    }

    /**
     * @return the incluso
     */
    public Boolean isIncluso() {
        return incluso;
    }

    /**
     * @param incluso the incluso to set
     */
    public File setIncluso(Boolean incluso) {
        this.incluso = incluso;
        return this;
    }
    
    public File inserisciLinkIn(TipoLinkIn link){
        if(link != null && link.getFile() == this)
            ManagerIn.inserisci(link);
        return this;
    }
    
    public File eliminaLinkIn(TipoLinkIn link){
        if(link != null && link.getFile() == this)
            ManagerIn.elimina(link);
        return this;
    }
    
    public TipoLinkIn getLinkIn() {
        return linkIn;
    }
    
    public void inserisciPerManagerIn(ManagerIn aThis) {
        if(aThis != null) linkIn = aThis.getLink();
    }

    public void eliminaPerManagerIn(ManagerIn aThis) {
        if(aThis != null) linkIn = null;
    }
    
    public File createOnDB(){
        try {
            FileDAO.create(this);
        } catch (EccezioneUpdater ex) {
            GUIUtils.showError(ex.getMessage());
        }
        return this;
    }
    
    public File readFromDB(){
        try {
            FileDAO.read(this);
        } catch (EccezioneUpdater ex) {
            GUIUtils.showError(ex.getMessage());
        }
        return this;
    }
    
    public File updateOnDB() throws EccezioneUpdater{
        FileDAO.update(this);
        return this;
    }
    
    public File deleteFromDB() throws EccezioneUpdater{
        FileDAO.delete(this);
        return this;
    }
    
    @Override
    public String toString(){
        return path_file.replaceAll(linkIn.getRoot().getPath(), new String()).substring(1);
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final File other = (File) obj;
        if ((this.path_file == null) ? (other.path_file != null) : !this.path_file.equals(other.path_file)) {
            return false;
        }
        return true;
    }

    @Override
    public int hashCode() {
        int hash = 7;
        hash = 73 * hash + (this.path_file != null ? this.path_file.hashCode() : 0);
        return hash;
    }
    
    
}
